package io.github.idol.roomlist

import android.app.Application
import android.arch.lifecycle.AndroidViewModel
import android.arch.lifecycle.MutableLiveData
import android.util.Log
import io.github.idol.data.ApiRepository
import io.github.idol.data.RoomListInfo
import io.github.idol.utils.RxJavaUtils

class RoomListViewModel(
        application: Application,
        private val apiRepository: ApiRepository
):AndroidViewModel(application){

    private val roomListData = MutableLiveData<List<RoomListInfo.ContentBean>>()

    fun getRoomListData():MutableLiveData<List<RoomListInfo.ContentBean>>{
        return roomListData
    }

    fun start(){
        ApiRepository.INSTANCE.getRoomList()
                .compose(RxJavaUtils.flowableAppleNetworkSchedulers())
                .map({
                    it.content?.forEach({
                        it.roomAvatar = ApiRepository.IMAGE_BASE_URL+it.roomAvatar
                    })
                    it
                })
                .subscribe({
                    Log.i("tag","$it.content?.size")
                    if(it.status == 401){

                    }else{
                        roomListData.value = it.content?.sortedByDescending {
                            it.commentTimeMs
                        }
                    }
                },Throwable::printStackTrace)
    }
}
